package top.chendawei.demo.system.web

import org.springframework.data.repository.query.Param
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import top.chendawei.demo.system.entity.Demo
import top.chendawei.demo.system.mapper.DemoRepository

@RestController
@RequestMapping("/demo")
class GreetingController(private val demoRepository: DemoRepository) {

    @GetMapping("/index")
    fun index(@Param(value = "s") name: String): String {
        return "index$name"
    }

    @GetMapping("/all")
    fun getAllUsers(): ResponseEntity<List<Demo>> {
        return ResponseEntity.ok(demoRepository.findAll())
    }

    @GetMapping("/{id}")
    fun getUserById(@PathVariable id: Long): ResponseEntity<Demo> {
        return demoRepository.findById(id)
            .map { ResponseEntity.ok(it) }
            .orElseGet { ResponseEntity.notFound().build() }
    }

    @GetMapping("/add")
    fun add(): ResponseEntity<Demo> {
        val m = Demo()
        m.name = "chendawei"
        m.email = "" + System.currentTimeMillis() + "aa@128.com"
        return ResponseEntity.ok(demoRepository.save(m))
    }
}